//
//  TopicDetailListViewController.m
//  cookBook
//
//  Created by xalo on 16/1/22.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "TopicDetailListViewController.h"
#import "WebViewController.h"
#define kUrl_list @"http://ibaby.ipadown.com/api/food/food.topic.detail.php?id="
#import "HTTPHelper_topic.h"
#import "TopicListCell.h"
#import "Model.h"
#import "HeaderView.h"
#import <MJRefresh.h>
@interface TopicDetailListViewController ()

@property (nonatomic, strong) NSMutableArray *listArray; //用于存储列表信息数组
@property (nonatomic, strong) Model *headModel; //存储表头信息的model


@end

@implementation TopicDetailListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置数据
    [self setNetData];
    
    //设置左按钮
    [self setLeftButton];
    
    
}


//设置左返回按钮
-(void)setLeftButton
{

    UIImage *backImage = [[UIImage imageNamed:@"btn_nav_back"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backImage style:(UIBarButtonItemStyleDone) target:self action:@selector(backToPreviousPage)];

}

//点击返回按钮方法实现
-(void)backToPreviousPage
{
    [self.navigationController popToRootViewControllerAnimated:YES];

}


//设置表头视图
-(void)setHeaderView
{
    
    CGFloat adjustH = [HeaderView HeaderViewHeightForModel:self.headModel];
    
    HeaderView *head = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, kWidth, adjustH)];
    
    head.model = self.headModel;
    
    self.tableView.tableHeaderView = head;


}



//设置数据
-(void)setNetData
{
    //拼接本页面的UrlStr
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",kUrl_list,self.ID];
    
    __weak TopicDetailListViewController *weakSelf = self;
    
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
        
        [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            
            //获取字典对应的model,并赋值给model
            NSDictionary *dic = responseObject;
            
            [weakSelf.headModel setValuesForKeysWithDictionary:dic];
            
            //一旦数据请求下来,调用设置表头视图
            [weakSelf setHeaderView];

            //tlist对应
            NSArray *tlistArray = responseObject[@"tlist"];
            
            NSDictionary *tlistDic = tlistArray[0];
            
            //取出list对应的数组,然后遍历
            NSArray *listArray = tlistDic[@"list"];
            
            //遍历数组,将字典转化model,存入数组中
            for (NSDictionary *dic in listArray) {
                
                Model *model = [[Model alloc] init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [weakSelf.listArray addObject:model];
                
                [weakSelf.tableView reloadData];
                
            }
            
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    
    

}


//懒加载数组
-(NSMutableArray *)listArray
{
    if (_listArray == nil) {
        
        _listArray = [NSMutableArray array];
        
    }
    return _listArray;

}

//懒加载model
-(Model *)headModel
{
    if (_headModel == nil) {
        _headModel = [[Model alloc] init];
    }
    return _headModel;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TopicListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listCell" forIndexPath:indexPath];
    
    //获取index对应的model
    Model *model = self.listArray[indexPath.row];
    
    //设置cell
    [cell setContentViewWithModel:model];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    Model *model = self.listArray[indexPath.row];
    
    WebViewController *webVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"WebVC"];
    
    webVC.model = model;
    
    [self.navigationController pushViewController:webVC animated:YES];

    
   
    
}





/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
